package biolean;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import biolean.commands.Command;
import biolean.files.BioFile;
import biolean.logic.GraphicElement;

public class Utils
{
	
	public static DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
	public static String newline = "\n";//System.getProperty("line.separator");
	
	public static boolean isSubClass(Class<?> parentClass, Class<?> childClass)
	{
		Class<?> currentClass = childClass;
		while (currentClass != null)
		{
			if (currentClass == parentClass)
				return true;
			currentClass = currentClass.getSuperclass();
		}
		return false;
	}
	
	public static boolean isBioFile(GraphicElement element)
	{
		return isSubClass(BioFile.class , element.getClass() );	
	}
	
	public static boolean isCommand(GraphicElement element)
	{
		return isSubClass(Command.class , element.getClass() );	
	}
	
	
	
	public static int findIndex(List<GraphicElement> objects, GraphicElement obj )
	{
		for (int i = 0 ; i <  objects.size(); i++)
		{
			if ( objects.get(i).equals(obj) )
				return i;
		}
		return -1;
	}
	
	public static String getExtenstion(String fileName)
	{
		String extension = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length()).toLowerCase();
		return extension;
	}
	
	public static Node findNode(NodeList nodeList, String key)
	{
		Node result = null;
		for (int i = 0 ; i < nodeList.getLength() ; i++)
		{
			Node node = nodeList.item(i);
			if (node.getNodeName().equals( key) )
			{
				result = node;
				break;
			}
		}
		return result;
	}	
	
	public static List<Node> findNodes(NodeList nodeList, String key)
	{
		List<Node> result = new ArrayList<Node>();
		for (int i = 0 ; i < nodeList.getLength() ; i++)
		{
			Node node = nodeList.item(i);
			if (node.getNodeName().equals( key) )
			{
				result.add(node);
			}
		}
		return result;
	}	
	
	public static String trimPath(String path)
	{
		int index = path.lastIndexOf( File.separator );
		if ( index != -1)
		{
			path = path.substring(index + 1 ,  path.length() );
		}
		return path;
	}
}
